
'use strict';

const BaseService = require("./base");


class CommentService extends BaseService{
   // 根据文章获取评论
    async getCommentByArticle(body){
        const {articleId} = body
        const res = await this.app.model.Comment.findAll({
            where:{article_id:articleId},
            attributes:{exclude:['delete_time']},
        })
        console.log(res,`1008611`);
        const reply_source_id = []
        res.forEach(item=>{
            if(!reply_source_id.includes(item.dataValues.from_user_id)){
                reply_source_id.push(item.dataValues.from_user_id)
            }
        })
        // 查出用户信息(头像和id)
        const reply_source_msg = await this.app.model.AdminUser.findAll({
            where:{id:reply_source_id},
            attributes:['id','avatar'],
        })
        res.forEach(item=>{
            reply_source_msg.forEach(data=>{
                if(item.dataValues.from_user_id == data.dataValues.id){
                    item.dataValues.avatar = data.dataValues.avatar
                }
            })
        })
        return res
    }

    // 根据评论 id 获取回复列表
    async getReplyByComment(body){
        const {commentId} = body
        const res = await this.app.model.Comment.findAll({
            where:{parent_id:commentId},
            attributes:{exclude:['delete_time']},
        })
        const reply_source_id = []
        res.forEach(item=>{
            if(!reply_source_id.includes(item.dataValues.from_user_id)){
                reply_source_id.push(item.dataValues.from_user_id)
            }
        })
        // 查出用户信息(头像和id)
        const reply_source_msg = await this.app.model.AdminUser.findAll({
            where:{id:reply_source_id},
            attributes:['id','avatar'],
        })
        res.forEach(item=>{
            reply_source_msg.forEach(data=>{
                if(item.dataValues.from_user_id == data.dataValues.id){
                    item.dataValues.avatar = data.dataValues.avatar
                }
            })
        })
        return res
    }

    // 发表评论回复
    async createComment(body){
        const params = this.app._.cloneDeep(body)
        params.create_time = new Date()
        await this.app.model.Comment.create(params)

        let commentMsg = '' // 回复的评论id
        let articleId = params.article_id // 文章id
        if(params.parent_id){
            const comment = await this.app.model.Comment.findOne({
                where:{
                    id:params.parent_id
                }
            })
            console.log(comment.dataValues);
            commentMsg =comment.content.length >10? comment.content.substring(0,10)+'...':comment.content
            articleId = comment.article_id
        }
        const user = await this.app.model.AdminUser.findOne({
            where:{id:params.from_user_id}
        })

        let MsgContent = commentMsg?`【${commentMsg.substring(0,5)+'...'}】${user.username}回复了你`:`你收到一条来自${user.username}的评论`
        const postData = {
            type:params.parent_id?3:2,
            content:MsgContent,
            article_id:articleId,
            user_id:params.to_user_id
        }
        await this.ctx.service.message.createMsg(postData)
    }   

    // 删除评论
    async deleteComment(body){
        await this.app.model.Comment.destroy({
            where:{id:body.id}
        })
    }
}


module.exports = CommentService;